package com.smile.stream.stream;

import com.smile.stream.StreamUtil;
import com.smile.stream.pojo.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 17:36
 */
public class StreamDemo3 {
    public static void main(String[] args) {
        map1();
        map2();
        map3();
        flatMap();


    }

    /**
     * 给定单词列表 ["Hello","World"]，你想要返回列表["H","e","l", "o","W","r","d"]。
     * 数据切割，去重
     */
    private static void flatMap() {
        String[] arrayOfWords = {"Hello", "World"};
        List<String> collect = Arrays.asList(arrayOfWords)
                .stream()
                .map(w -> w.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(collect);



    }

    private static void map2() {
        List<String> words = Arrays.asList("Java 8", "Lambdas", "In", "Action");
        List<Integer> wordLengths = words.stream()
                .map(String::length)
                .collect(Collectors.toList());
        System.out.println(wordLengths);
    }

    private static void map3() {
        List<Integer> collect = StreamUtil.getMenu().stream()
                .map(Dish::getName)
                .map(String::length)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    private static void map1() {
        List<String> dishNames = StreamUtil.getMenu().stream()
                .map(Dish::getName)
                .collect(Collectors.toList());
        System.out.println(dishNames);

    }
}
